home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / TransSkel / Demos / Pascal Demos / ManyWind / ManyWind.p next >
Text File  |  1994-02-23  |  5KB  |  256 lines

  1. program ManyWind;
  2.  
  3.     uses
  4.         TransSkel;
  5.  
  6.     const
  7.  
  8.         maxWind = 20;
  9.  
  10. { menu numbers }
  11.         aMenuNum = skelAppleMenuID;
  12.         fMenuNum = aMenuNum + 1;
  13.         wMenuNum = fMenuNum + 1;
  14.         cMenuNum = wMenuNum + 1;
  15.  
  16. { File menu item numbers }
  17.         newWind = 1;
  18.         closeWind = 2;
  19.         quitApp = 4;
  20.  
  21. { Color menu item numbers }
  22.         cWhite = 1;
  23.         cLtGray = 2;
  24.         cGray = 3;
  25.         cDkGray = 4;
  26.         cBlack = 5;
  27.  
  28.     var
  29.         fileMenu: MenuHandle;
  30.         windowMenu: MenuHandle;
  31.         colorMenu: MenuHandle;
  32.  
  33.         windCount: Integer;        { number of currently existing windows }
  34.         windNum: Integer;        { id of last window created }
  35.  
  36.     procedure MakeWindow;
  37.     forward;
  38.  
  39.     procedure DoFileMenu (item: Integer);
  40.         var
  41.             w: WindowPtr;
  42.     begin
  43.         case item of
  44.             newWind: 
  45.                 MakeWindow;
  46.             closeWind: 
  47.                 SkelClose(FrontWindow);
  48.             quitApp: 
  49.                 SkelStopEventLoop;
  50.         end;
  51.     end;
  52.  
  53.  
  54.     procedure DoWindowMenu (item: Integer);
  55.         var
  56.             iTitle: Str255;
  57.             wTitle: Str255;
  58.             w: WindowPtr;
  59.     begin
  60.         GetItem(windowMenu, item, iTitle);        { get window name }
  61.         w := FrontWindow;
  62.         while (w <> nil) do
  63.             begin
  64.                 GetWTitle(w, wTitle);
  65.                 if (EqualString(iTitle, wTitle, false, true)) then
  66.                     begin
  67.                         SelectWindow(w);
  68.                         w := nil;
  69.                     end
  70.                 else
  71.                     w := WindowPtr(WindowPeek(w)^.nextWindow);
  72.             end;
  73.     end;
  74.  
  75.  
  76.     procedure DoColorMenu (item: Integer);
  77.         var
  78.             w: WindowPtr;
  79.     begin
  80.         w := FrontWindow;
  81.         if (WindowPeek(w)^.windowKind < 0) then    { front is DA window }
  82.             exit(DoColorMenu);
  83.         case item of
  84.             cWhite: 
  85.                 BackPat(white);
  86.             cLtGray: 
  87.                 BackPat(ltGray);
  88.             cGray: 
  89.                 BackPat(gray);
  90.             cDkGray: 
  91.                 BackPat(dkGray);
  92.             cBlack: 
  93.                 BackPat(black);
  94.         end;
  95.         EraseRect(w^.portRect);
  96.         SetWRefCon(w, item);
  97.     end;
  98.  
  99.  
  100.     procedure DoMClobber (m: MenuHandle);
  101.     begin
  102.         DisposeMenu(m);
  103.     end;
  104.  
  105.  
  106.     procedure SetItemEnableState (m: MenuHandle;
  107.                                     item: Integer;
  108.                                     state: Boolean);
  109.     begin
  110.         if (state) then
  111.             EnableItem(m, item)
  112.         else
  113.             DisableItem(m, item);
  114.     end;
  115.  
  116.  
  117.     procedure AdjustMenus;
  118.         var
  119.             nItems: Integer;
  120.             i: Integer;
  121.             iTitle: Str255;
  122.             wTitle: Str255;
  123.             mark: Byte;
  124.     begin
  125.         SetItemEnableState(fileMenu, newWind, windCount < maxWind);
  126.         SetItemEnableState(fileMenu, closeWind, FrontWindow <> nil);
  127.         if (windCount > 0) then
  128.             begin
  129.                 for i := 1 to 5 do
  130.                     begin
  131.                         if (GetWRefCon(FrontWindow) = i) then
  132.                             mark := checkMark
  133.                         else
  134.                             mark := noMark;
  135.                         SetItemMark(colorMenu, i, char(mark));
  136.                     end;
  137.                 GetWTitle(FrontWindow, wTitle);
  138.                 nItems := CountMItems(windowMenu);
  139.                 for i := 1 to nItems do
  140.                     begin
  141.                         GetItem(windowMenu, i, iTitle);
  142.                         if (EqualString(iTitle, wTitle, false, true)) then
  143.                             mark := checkMark
  144.                         else
  145.                             mark := noMark;
  146.                         SetItemMark(windowMenu, i, char(mark));
  147.                     end;
  148.             end;
  149.     end;
  150.  
  151.  
  152.     procedure DoWUpdate (resized: Boolean);
  153.         var
  154.             w: WindowPtr;
  155.     begin
  156.         GetPort(w);
  157.         EraseRect(thePort^.portRect);    { repaint w/background pattern }
  158.     end;
  159.  
  160.  
  161.     procedure DoWClose;
  162.         var
  163.             w: WindowPtr;
  164.     begin
  165.         GetPort(w);                        { window to be closed }
  166.         SkelRmveWind(w);
  167.     end;
  168.  
  169.  
  170.     procedure DoWClobber;
  171.         var
  172.             w: WindowPtr;
  173.             i: Integer;
  174.             mItems: Integer;
  175.             iTitle: Str255;
  176.             wTitle: Str255;
  177.     begin
  178.         GetPort(w);            { window to be close }
  179.         GetWTitle(w, wTitle);
  180.         DisposeWindow(w);
  181.         windCount := windCount - 1;
  182.         if (windCount = 0) then
  183.             begin
  184.                 SkelRmveMenu(windowMenu);    { last window - clobber menus }
  185.                 SkelRmveMenu(colorMenu);
  186.             end
  187.         else
  188.             begin
  189.                 mItems := CountMItems(windowMenu);
  190.                 for i := 1 to mItems do
  191.                     begin
  192.                         GetItem(windowMenu, i, iTitle);
  193.                         if (EqualString(iTitle, wTitle, false, true)) then
  194.                             DelMenuItem(windowMenu, i);
  195.                     end;
  196.             end;
  197.     end;
  198.  
  199.  
  200.     procedure MakeWindow;
  201.         var
  202.             w: WindowPtr;
  203.             r: Rect;
  204.             s: Str255;
  205.             ignore: Boolean;
  206.     begin
  207.         w := FrontWindow;
  208.         if (w = nil) then
  209.             SetRect(r, 100, 100, 300, 250)
  210.         else
  211.             begin
  212.                 SkelGetWindContentRect(w, r);
  213.                 OffsetRect(r, 20, 20);
  214.                 if ((r.left > 480) or (r.top > 300)) then    { keep on screen }
  215.                     OffsetRect(r, 40 - r.left, 40 - r.top);
  216.             end;
  217.         windNum := windNum + 1;
  218.         NumToString(windNum, s);
  219.         if (SkelQuery(skelQHasColorQD) <> 0) then
  220.             w := NewCWindow(nil, r, s, true, noGrowDocProc, WindowPtr(-1), true, 0)
  221.         else
  222.             w := NewWindow(nil, r, s, true, noGrowDocProc, WindowPtr(-1), true, 0);
  223.         ignore := SkelWindow(w, nil, nil, @DoWUpdate, nil, @DoWClose, @DoWClobber, nil, false);
  224.         windCount := windCount + 1;
  225.         if (windCount = 1) then        { if first window, create new menus }
  226.             begin
  227.                 colorMenu := NewMenu(cMenuNum, 'Color');
  228.                 AppendMenu(colorMenu, 'White;Light Gray;Gray;Dark Gray;Black');
  229.                 ignore := SkelMenu(colorMenu, @DoColorMenu, @DoMClobber, false, false);
  230.                 windowMenu := NewMenu(wMenuNum, 'Window');
  231.                 ignore := SkelMenu(windowMenu, @DoWindowMenu, @DoMClobber, false, true);
  232.             end;
  233.         AppendMenu(windowMenu, s);
  234.         SetWRefCon(w, cWhite);
  235.     end;
  236.  
  237.  
  238.     procedure SetupMenus;
  239.         var
  240.             ignore: Boolean;
  241.     begin
  242.         SkelApple('', nil);
  243.         fileMenu := NewMenu(fMenuNum, 'File');
  244.         AppendMenu(fileMenu, 'New/N;Close/W;(-;Quit/Q');
  245.         ignore := SkelMenu(fileMenu, @DoFileMenu, @DoMClobber, false, true);
  246.     end;
  247.  
  248.  
  249. begin
  250.     windCount := 0;
  251.     windNum := 0;
  252.     SkelInit(nil);
  253.     SetupMenus;
  254.     SkelEventLoop;
  255.     SkelCleanup;
  256. end.